$(function () {

    //异步请求新闻类型
    $.ajax({
        type: 'get',
        url: '/newsType',
        data: {},
        success: function (data) {
            if (data.message == "success") {
                $(data.list).each(function (index,item) {
                    let newsType = "<div>\n" +
                        "                    <a href=\"#\" onclick=\"findById("+item.id+",'/html/news_and_trends.html')\">\n" +
                        "                        <span>"+item.name+"</span>\n" +
                        "                    </a>\n" +
                        "                </div>";
                    $("#news-nav").append(newsType);
                });
                return false;
            }
            layer.msg('查询失败，请稍后重试！', {icon: 2, time: 1200});
            return false;
        },
        error: function () {
            layui.layer.msg('请求接口异常,请刷新重试！',{icon:0,time:1300});
            return false;
        }
    })
    var id = sessionStorage.getItem("id");
    if (id == null) {
        $("#newsdtl").html("暂无数据！！！");
        $("#pageNews").html("");
        return false;
    }
    //根据新闻Id查询新闻详情
    findByIdNesDetail(id);

});

// 修改新闻浏览次数五秒后执行
setTimeout("findByIdUpdateHits()",5000);

/**
 * 根据新闻Id查询新闻详情
 * @param id
 */
function findByIdNesDetail(id) {
    //发异步，通过Id获取新闻详细内容
    $.ajax({
        type: 'get',
        url: '/news/NewsFindById',
        data: {'id':id},
        success: function (res) {
            if (res.message == "success") {
                console.log(res);
                if (res.newsList.length == 0) {
                    $("#newsdtl").html("暂无数据！！！");
                    $("#pageNews").html("");
                    return false;
                }
                if (res.newsList.length == 2) {
                    // 匹配成功就是详情数据
                    if (res.newsList[0].id == id) {
                        let {title, subhead, content, author, releaseDate, hits} = res.newsList[0];
                        $("#news-title").html(title);
                        $("#news-author").html(author);
                        $("#news-releaseDate").html(releaseDate);
                        $("#news-hits").html(hits);
                        $("#news-subhead").html(subhead);
                        $("#news-content").html(content);
                        let pageNews = "<div>\n" +
                            "                    <a href=\"javascript:void(0);\" >\n" +
                            "                        上一篇：<span>无</span>\n" +
                            "                    </a>\n" +
                            "                </div>\n" +
                            "                <div>\n" +
                            "                    <a href=\"javascript:void(0);\" onclick=\"findById(" + res.newsList[1].id + ",'/html/news_details.html')\" >\n" +
                            "                        下一篇：<span>" + res.newsList[1].title + "</span>\n" +
                            "                    </a>\n" +
                            "                </div>";
                        $("#pageNews").html(pageNews);
                        return false;
                    } else {
                        let {title, subhead, content, author, releaseDate, hits} = res.newsList[1];
                        $("#news-title").html(title);
                        $("#news-author").html(author);
                        $("#news-releaseDate").html(releaseDate);
                        $("#news-hits").html(hits);
                        $("#news-subhead").html(subhead);
                        $("#news-content").html(content);
                        let pageNews = "<div>\n" +
                            "                    <a href=\"javascript:void(0);\" onclick=\"findById(" + res.newsList[0].id + ",'/html/news_details.html')\"  >\n" +
                            "                        上一篇：<span>" + res.newsList[0].title + "</span>\n" +
                            "                    </a>\n" +
                            "                </div>\n" +
                            "                <div>\n" +
                            "                    <a href=\"javascript:void(0);\" >\n" +
                            "                        下一篇：<span>无</span>\n" +
                            "                    </a>\n" +
                            "                </div>";
                        $("#pageNews").html(pageNews);
                        return false;
                    }
                } else {
                    let {title, subhead, content, author, releaseDate, hits} = res.newsList[1];
                    $("#news-title").html(title);
                    $("#news-author").html(author);
                    $("#news-releaseDate").html(releaseDate);
                    $("#news-hits").html(hits);
                    $("#news-subhead").html(subhead);
                    $("#news-content").html(content);
                    let pageNews = "<div>\n" +
                        "                    <a href=\"javascript:void(0);\" onclick=\"findById(" + res.newsList[0].id + ",'/html/news_details.html')\" >\n" +
                        "                        上一篇：<span>"+res.newsList[0].title+"</span>\n" +
                        "                    </a>\n" +
                        "                </div>\n" +
                        "                <div>\n" +
                        "                    <a href=\"javascript:void(0);\" onclick=\"findById(" + res.newsList[2].id + ",'/html/news_details.html')\" >\n" +
                        "                        下一篇：<span>" + res.newsList[2].title + "</span>\n" +
                        "                    </a>\n" +
                        "                </div>";
                    $("#pageNews").html(pageNews);
                    return false;
                }
                return false;
            }
            layer.msg('查询失败，请刷新重试！', {icon: 2, time: 1200});
            return false;
        },
        error: function () {
            layer.msg('请求接口异常，请刷新重试！', {icon: 0, time: 1200});
            return false;
        }
    });
}


/**
 * 三秒后出发请求
 */
function findByIdUpdateHits() {
    var id = sessionStorage.getItem("id");
    if (id == null) {
        $("#newsdtl").html("暂无数据！！！");
        $("#pageNews").html("");
        return false;
    }
    //发异步，通过Id获取新闻详细内容
    $.ajax({
        type: 'post',
        url: '/news/newsUpdate',
        data: {'id':id},
        success: function (res) {
            if (res.message == "success") {
                console.log("浏览成功！");
                return false;
            }
            layer.msg('查询失败，请刷新重试！', {icon: 2, time: 1200});
            return false;
        },
        error: function () {
            layer.msg('请求接口异常，请刷新重试！', {icon: 0, time: 1200});
            return false;
        }
    });

}